﻿<?php
	/*Connect database class [11/27/2012]*/
	class connect_database
	{
		private static $instance;
		
		private function __construct()
		{
			
		}
		
		public static function get_instance()
		{
			if(self::$instance == NULL)
			 	self::$instance = new self();
			return self::$instance;
		}
		
		public function open()
		{
			if(mysql_connect("localhost", "root", "") && mysql_select_db("danchinhdang"))
				return true;				
			else
				return false;
		}
		
		public function close()
		{
			mysql_close();
		}
		
	}
	
	/*Execute mysql query class [11/27/2012]*/
	class execute_mysql_query
	{	
		public function __construct()
		{
			
		}
		
		/*
		0: Return excute query result(insert, delete, update: true/false; select: result set)
		1: Return first cell of first row
		2: Return number of row affect
		*/
		public function execute($query, $return_type)
		{
			if(connect_database::get_instance()->open())
			{
				mysql_query("set names 'utf8'");
				$result = mysql_query($query);
				if(!$result)
				{
					/*Use for debug - Comment when publish*/
					print("Error execute query: " . mysql_error());
				}
				else
				{
					switch($return_type)
					{
						case 0:
							break;
						case 1:
							$result = @mysql_result($result, 0);
							break;
						case 2:
							$result = mysql_num_rows($result);
							break;
						default:
					}
				}
				connect_database::get_instance()->close();
				return $result;
			}
			else
			{
				print("Can not connect to server!");
			}
		}
		
		public function escape_string($str_arry)
		{
			if(connect_database::get_instance()->open())
			{
				$size = count($str_arry);
				for($i = 0; $i < $size; $i++)
				{
					$str_arry[$i] = mysql_real_escape_string($str_arry[$i]);
				}
				connect_database::get_instance()->close();
				return $str_arry;
			}
			else
			{
				print("Can not connect to server!");
			}
		}
		
		function __destruct()
		{			
		}
	}
	
	/*Global function class [11/27/2012]*/
	class global_function
	{
		public function __construct()
		{
		}
		
		public function create_ramdom_string($length)
		{
			$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
			$str = NULL;
			$size = strlen($chars);
			for($i = 0; $i < $length; $i++) 
			{
				$str .= $chars[rand(0, $size - 1)];
			}
			return $str;
		}
		
		public function create_ramdom_number($length)
		{
			$chars = "0123456789";
			$str = NULL;
			$size = strlen($chars);
			for($i = 0; $i < $length; $i++) 
			{
				if($i == 0 && $chars[rand(0, $size - 1)] == 0)
					$i--;
				else
					$str .= $chars[rand(0, $size - 1)];
			}
			return $str;
		}
		
		public function send_mail($to, $subject, $message)
		{
			//To send HTML mail, the Content-type header must be set
			$headers  = 'MIME-Version: 1.0' . "\r\n";
			$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
			
			// Additional headers
			$headers .= 'From: Tranh Theu Chu Thap <dohuute@gmail.com>' . "\r\n";
			$headers .= 'Reply-To: dohuute@gmail.com' . "\r\n";
			$headers .= 'X-Mailer: PHP/' . phpversion();
			
			return mail($to, $subject, $message, $headers);
		}
		
		public function send_SMTP_mail($to_email, $to_name, $from_email, $from_name, $subject, $message)
		{
			include_once('class.phpmailer.php');
			$mail = new PHPMailer();
			$mail->CharSet = 'UTF-8';
			$mail->IsSMTP();
			$mail->Host = 'localhost';
			$mail->SMTPAuth = true;
			$mail->Username = 'email@domain.com';
			$mail->Password = 'password email';
			$mail->SetFrom($from_email, $from_name);
			$mail->AddAddress($to_email, $to_name);
			$mail->IsHTML(true);
			$mail->Subject = $subject;
			$mail->Body = $message;
			return $mail->Send();
		}
		
		public function check_admin_authenticate()
		{
			return isset($_SESSION["IS_ADMIN"]) && isset($_SESSION["FULL_NAME"]) && isset($_SESSION["USER_NAME"]) && $_SESSION["IS_ADMIN"];
		}
		
		public function read_money($money)
		{
			if($money == 0)
				return "Không đồng";
			$read = array(NULL, NULL, "ngàn", "triệu", "tỷ");
			$chu_so = NULL;
			if(strlen($money) > 3)
			{
				$chieu_dai = strlen($money);
				$so_lan_lap = floor($chieu_dai / 3);
				$chieu_dai_lay_lan_cuoi = $chieu_dai % 3;
				for($i = 1; $i <= $so_lan_lap; $i++)
				{
					$ba_so = substr($money, $chieu_dai - ($i * 3), 3);
					if($ba_so != "000")
						$chu_so = $this->tach($ba_so)." ". $read[$i]. " ". $chu_so;
				}
				
				if($chieu_dai_lay_lan_cuoi > 0)
				{
					$so_du = substr($money, 0, $chieu_dai_lay_lan_cuoi);
					$chu_so = $this->tach($so_du)." ". $read[$i]. " ".$chu_so;
				}
				return ucfirst(trim($chu_so)). " đồng";
			}
			else
				return ucfirst(trim($this->tach($money))). " đồng";
		}
		
		private function tach($num)
		{
			$chu_so = array("không", "một", "hai", "ba", "bốn", "năm", "sáu", "bảy", "tám", "chín");
			if($num == "000")
				return NULL;
				
			if(strlen($num) == 3)
			{
				$tram = substr($num, 0, 1);
				$tram = $chu_so[$tram]." trăm";
				
				if(substr($num, 1, 2) == "00")
					return $tram;
					
				$chuc = substr($num, 1, 1);
				if($chuc == 0)
					$chuc = "lẻ";
				else if($chuc == 1)
					$chuc = " mười";
				else
					$chuc = $chu_so[$chuc]. " mươi";
				
				$dv = substr($num, 2, 1);
				if($dv != 0)
					$dv = $chu_so[$dv];
				else
					$dv = NULL;
				return $tram." ".$chuc." ".$dv;
			}
			else if(strlen($num) == 2)
			{
				$chuc = substr($num, 0, 1);
				if($chuc == 0)
					$chuc = NULL;
				else if($chuc == 1)
					$chuc = "mười";
				else
					$chuc = $chu_so[$chuc]. " mươi";
				
				$dv = substr($num, 1, 1);
				if($dv != 0)
					$dv = $chu_so[$dv];
				else
					$dv = NULL;
				return $chuc." ".$dv;
			}
			else
			{
				return $chu_so[$num];
			}
		}
		
		public function remove_sign($str)
		{
			$str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", 'a', $str);
			$str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $str);
			$str = preg_replace("/(ì|í|ị|ỉ|ĩ)/", 'i', $str);
			$str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", 'o', $str);
			$str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $str);
			$str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", 'y', $str);
			$str = preg_replace("/(đ)/", 'd', $str);
			$str = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/", 'A', $str);
			$str = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", 'E', $str);
			$str = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ)/", 'I', $str);
			$str = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/", 'O', $str);
			$str = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", 'U', $str);
			$str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", 'Y', $str);
			$str = preg_replace("/(Đ)/", 'D', $str);
			$str = trim($str);
			$str = str_replace(" ", "-", str_replace("&*#39;","",$str));
			$str = str_replace(",", "", $str);
			$str = str_replace("%", "", $str);
			$str = strtolower($str);
			return $str;
		}
		
		public function check_session()
		{
			if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 1800))
			{
				// last request was more than 30 minutes ago
				session_unset();     // unset $_SESSION variable for the run-time 
				session_destroy();   // destroy session data in storage
			}
			$_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp
		}
		
		public function get_begin($current_page, $row_per_page, $total_record)
		{
			$total_page = ceil($total_record/$row_per_page);
			
			if($current_page > $total_page)
				$current_page = $total_page;
				
			if(!isset($current_page) || $current_page <= 0)
				$current_page = 1;
			
			return $current_page * $row_per_page - $row_per_page;
		}
		
		public function paging($total_record, $row_per_page)
		{
			$cur_page = str_replace('-', '', $_GET['page']);
			$total_page = ceil($total_record/$row_per_page);
			
			if($cur_page <= 0)
				$cur_page = 1;
			if($cur_page > $total_page)
				$cur_page = $total_page;
				
			$uri = $_SERVER['REQUEST_URI'];
			$pos1 = strrpos($uri, '-');
			
			if($pos1 !== false)
			{
				$pos2 = strrpos($uri, '.');
				if(is_numeric(substr($uri, $pos1+1, $pos2-$pos1-1)))
				{
					$uri = substr($uri, 0, $pos1);
					$uri.='.html';
				}
			}
			
			//echo $uri;
			
			$uri_arr = explode('.', $uri);
			
			//From
			if($cur_page - 2 <= 0)
			{
				$from = 1;
			}
			else
			{
				$from = $cur_page - 2;
				if($cur_page + 2 >= $total_page & $from > 1)
				{
					$from = $total_page - 4;
					if($from < 1)
						$from = 1;
				}
			}
			
			//To
			if($cur_page + 2 >= $total_page)
			{
				$to = $total_page;
			}
			else
			{
				$to = $cur_page + 2;
				if($to < 5)
				{
					$to = 6 - $from;
					if($to > $total_page)
						$to = $total_page;
				}
			}
			
			$paging = "<div class=\"paging_ct pos_f\" id=\"paging\">";
			if($cur_page - 1 > 1)
			{
				$paging .= "<div class='paging_button' id='first'><a href=\"".$uri_arr[0]."-1.".$uri_arr[1]."\" >First</a></div>";
			}
			if($cur_page > 1)
			{
				$paging .= "<div class='paging_button' id='prev'><a href=\"".$uri_arr[0]."-".($cur_page - 1).".".$uri_arr[1]."\" >Previous</a></div>";
			}
			if($cur_page - 2 > 1 && $from > 1)
			{
				$paging .= "<span class=\"span_more\"> ... </span>";
			}
			
			
			for($i = $from; $i <= $to; $i++)
			{
				if($i == $cur_page)
					$class ="class=\"current_paging_button\"";
				else
					$class ="class=\"paging_button\"";
					
				$paging .= "<div ".$class."><a href=\"".$uri_arr[0]."-".$i.".".$uri_arr[1]."\" >".$i."</a></div>";
			}
			if($cur_page + 2 < $total_page && $to < $total_page)
			{
				$paging .= "<span class=\"span_more\"> ... </span>";
			}
			if($cur_page < $total_page)
			{
				$paging .= "<div class='paging_button' id='next'><a href=\"".$uri_arr[0]."-". ($cur_page + 1) .".".$uri_arr[1]."\" >Next</a></div>";
			}
			if($cur_page + 1 < $total_page)
			{
				$paging .= "<div class='paging_button' id='last'><a href=\"".$uri_arr[0]."-".$total_page.".".$uri_arr[1]."\" >Last</a></div>";
			}
			$paging .= "</div>";
			if($total_page > 1)
				print($paging);
		}
		
		public function admin_paging($total_record, $row_per_page)
		{
			@$cur_page = $_GET['page'];
			if(!isset($cur_page))
				$cur_page = 1;
				
			$total_page = ceil($total_record/$row_per_page);
			
			if($cur_page <= 0)
				$cur_page = 1;
			if($cur_page > $total_page)
				$cur_page = $total_page;
				
			$uri = $_SERVER['REQUEST_URI'];
			$pos1 = strrpos($uri, '&page=');
			
			if($pos1 == true)
			{
				$uri = substr($uri, 0, $pos1);
			}
			//echo $pos1;
			//echo $uri;
			
			//From
			if($cur_page - 2 <= 0)
			{
				$from = 1;
			}
			else
			{
				$from = $cur_page - 2;
				if($cur_page + 2 >= $total_page & $from > 1)
				{
					$from = $total_page - 4;
					if($from < 1)
						$from = 1;
				}
			}
			
			//To
			if($cur_page + 2 >= $total_page)
			{
				$to = $total_page;
			}
			else
			{
				$to = $cur_page + 2;
				if($to < 5)
				{
					$to = 6 - $from;
					if($to > $total_page)
						$to = $total_page;
				}
			}
			
			$paging = "<div class=\"paging_ct pos_f\" id=\"paging\">";
			if($cur_page - 1 > 1)
			{
				$paging .= "<div class='paging_button' id='first'><a href=\"".$uri."&page=1\" >First</a></div>";
			}
			if($cur_page > 1)
			{
				$paging .= "<div class='paging_button' id='prev'><a href=\"".$uri."&page=".($cur_page - 1)."\" >Previous</a></div>";
			}
			if($cur_page - 2 > 1 && $from > 1)
			{
				$paging .= "<span class=\"span_more\"> ... </span>";
			}
			
			
			for($i = $from; $i <= $to; $i++)
			{
				if($i == $cur_page)
					$class ="class=\"current_paging_button\"";
				else
					$class ="class=\"paging_button\"";
					
				$paging .= "<div ".$class."><a href=\"".$uri."&page=".$i."\" >".$i."</a></div>";
			}
			if($cur_page + 2 < $total_page && $to < $total_page)
			{
				$paging .= "<span class=\"span_more\"> ... </span>";
			}
			if($cur_page < $total_page)
			{
				$paging .= "<div class='paging_button' id='next'><a href=\"".$uri."&page=". ($cur_page + 1)."\" >Next</a></div>";
			}
			if($cur_page + 1 < $total_page)
			{
				$paging .= "<div class='paging_button' id='last'><a href=\"".$uri."&page=".$total_page."\" >Last</a></div>";
			}
			$paging .= "</div>";
			if($total_page > 1)
				print($paging);
		}
		
		function check_upload_file($file, $file_type_arr)
		{
			/*"image/gif" "image/jpeg"  "image/png"  "image/x-png"  "image/pjpeg" "image/bmp"*/
			$valid = FALSE;
			//echo $_FILES[$file]["type"];
			foreach($file_type_arr as $ft)
			{
				if($_FILES[$file]["type"] == $ft)
				{
					$valid = TRUE;
					break;
				}
			}

			if(isset($_FILES[$file]["name"]) && $_FILES[$file]["error"] > 0)
				$result = array(FALSE, "error_upload");
			else if($_FILES[$file]["size"] >= 1024 * 1024)
				$result = array(FALSE, "error_file_size");
			else if(isset($_FILES[$file]["name"]) && !$valid)
				$result = array(FALSE, "error_file_type");
			else
				$result = array(TRUE, NULL);
			
			return $result;
		}
		
		function upload_file($file, $name, $loc)
		{
			$arr_name = explode(".", $_FILES[$file]["name"]);
			$ext = end($arr_name);
			return array(move_uploaded_file($_FILES[$file]["tmp_name"], $loc."/".$name.".".$ext), $name.".".$ext);
		}
		
		function delete_file($directory, $name)
		{
			$files = glob($directory."/".$name.".*");
			foreach($files as $file)
				unlink($file);
		}
		
		public function format_number($num, $num_after_point)
		{
			return number_format($num, $num_after_point, ',', '.');
		}
		
		function __destruct()
		{
		}
	}
	
	/*En/Decrypt string class [11/30/2012]*/
	class security
	{
		private $key;
		
		public function __construct()
		{
			$this->key = "HuynhTh@nhDu@Gm@il.Com";
		}
		
		public function encryption($str_to_enc)
		{
			return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($this->key), $str_to_enc, MCRYPT_MODE_CBC, md5(md5($this->key))));
		}
		
		public function decryption($str_to_dec)
		{
			return rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($this->key), base64_decode($str_to_dec), MCRYPT_MODE_CBC, md5(md5($this->key))), "\0");
		}
		
		function __destruct()
		{
			$this->key;
		}
	}
	
	/*Master page function [5/12/2013]*/
	class master_page
	{
		public function __construct()
		{
		}
		
		public function menu_generate($role_id, $group_id, &$num)
		{
			$exe_mysql = new execute_mysql_query();
			$query = "SELECT function.fun_id, name, url
				FROM function WHERE group_id = $group_id
				AND function.fun_id
				IN (
					SELECT rolehavefunction.fun_id
					FROM rolehavefunction
					WHERE rol_id = $role_id
				)";
			$num = $exe_mysql->execute($query, 2);
			return $result = $exe_mysql->execute($query, 0);
		}
		
		public function check_access($role_id, $fun_id)
		{
			$exe_mysql = new execute_mysql_query();
			$query = "SELECT rol_id
				FROM rolehavefunction
				WHERE rol_id = $role_id AND fun_id = $fun_id";
			return (bool)$exe_mysql->execute($query, 2);
		}
		
		/*
		**1: Hospital name
		**2: Phone
		**3: Fax
		**4: Address
		*/
		public function get_hospital_info($field)
		{
			switch($field)
			{
				case 1:
					$field_name = "hospital_name";
					break;
				case 2:
					$field_name = "phone";
					break;
				case 3:
					$field_name = "fax";
					break;
				case 4:
					$field_name = "address";
					break;
			}
			$exe_mysql = new execute_mysql_query();
			return $exe_mysql->execute("select ".$field_name." from hospitalinfo where id = 1", 1);
		}
		
		function __destruct()
		{
		}
	}
?>